package com.eaoa.query.pub;

import java.util.Collection;
import java.util.Iterator;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;

import com.eaoa.base.EABaseQuery;
import com.eaoa.vo.pub.PubAccount;
import com.eaoa.vo.pub.PubCustomer;

public class PubCustomerQuery extends EABaseQuery {

	boolean lazyInitPubCustomers = false;
	
	public PubCustomerQuery() {
		super(PubCustomer.class);
		addInnerJoin(1, "pubAccounts", "pubAccounts");
	}

	public void setLazyInitPubCustomers(boolean lazyInitPubCustomers) {
		this.lazyInitPubCustomers = lazyInitPubCustomers;
	}
	
	protected void lazeInit() throws HibernateException {
		Iterator it = getResults().iterator();
		while (it.hasNext()) {
			PubCustomer vo = (PubCustomer) it.next();
			if (lazyInitPubCustomers) {
				Hibernate.initialize(vo.getPubAccounts());
			}
		}
	}

	public void orderByCustomercode() {
		setASCOrder("customercode");
	}

	public void setMainregion(String val) {
		setEQCondition("mainregion", val);
	}

	public void setCustomerno(Long var) {
		setEQCondition("customerno", var);
	}

	public void setCustomerno(Collection var) {
		setINCondition("customerno", var);
	}

	public void setCustomercode(Collection val) {
		setINCondition("customercode", val);
	}

	public void setCustomercode(String val) {
		setEQCondition("customercode", val);
	}

	public void setLikeCustomercode(String val) {
		setLIKECondition("customercode", val);
	}

	public void setCustomercodeLike(String val) {
		setLIKECondition("customercode", val);
	}

	public void setNotCustomercode(String val) {
		setNOEQCondition("customercode", val);
	}

	public void setIslock(String val) {
		setEQCondition("islock", val);
	}

	public void setCustomertype(String type) {
		setBITANDCondition("customertype", type);
	}

	public void setCustomercnname(String val) {
		setLIKECondition("name", val);
	}

	public void setCreatedbyname(String val) {
		setLIKECondition("createdbyname", val);
	}

	public void setCreatedby(Long val) {
		setEQCondition("createby", val);
	}
	
	public void setRouteno(Long val) {
		setEQCondition("routeno", val);
	}

	public static PubCustomer loadByNo(Long customerno) {
		PubCustomerQuery query = new PubCustomerQuery();
		query.setCustomerno(customerno);
		query.execute();

		if (query.getResults().size() != 1)
			return null;

		return (PubCustomer) query.getResults().get(0);
	}

	public static PubCustomer loadByIdFetchPubAccounts(Long val) {
		PubCustomerQuery query = new PubCustomerQuery();
		query.setCustomerno(val);
		query.setLazy(true);
		query.setLazyInitPubCustomers(true);
		query.execute();

		if (query.getResults().size() == 0) {
			return null;
		} else {
			return (PubCustomer) query.getResults().get(0);
		}
	}

	public static PubCustomer loadByCode(String val) {
		PubCustomerQuery query = new PubCustomerQuery();
		query.setCustomercode(val);
		query.execute();

		if (query.getResults().size() == 0) {
			return null;
		} else {
			return (PubCustomer) query.getResults().get(0);
		}
	}

	public static PubCustomer loadByCustomercnname(String customercnname) {
		PubCustomerQuery query = new PubCustomerQuery();
		query.setCustomercnname(customercnname);
		query.setIslock("N");
		query.execute();

		if (query.getResults().size() != 1)
			return null;

		return (PubCustomer) query.getResults().get(0);
	}

}
